<template>
    <view>
        <form>
          <!-- <view class="cu-card margin radius">

            <view class="cu-form-group margin-top">
                <view class="title">项目编号</view>
                <view>{{ dataInfo.projectno }}</view>
            </view>
            <view class="cu-form-group ">
                <view class="title">项目名称</view>
                <view>{{ dataInfo.projectname }}</view>
            </view>

          </view> -->
          <xmInfo
      :projectno="dataInfo.projectno"
      :projectname="dataInfo.projectname"
    ></xmInfo>
            <view class="cu-form-group " style="margin-top: 10px;">
                <view class="title"><text class="cuIcon-favorfill text-xs text-red"></text
              >水流量(立方米)</view>
                <view><input :disabled="isDisabled" placeholder="请输入" name="input" v-model="dataInfo.discharge"  class="input-align" type="digit"></input></view>
            </view>
            <view class="cu-form-group ">
                <view class="title"><text class="cuIcon-favorfill text-xs text-red"></text
              >水压（MPa）</view>
                <view><input :disabled="isDisabled" placeholder="请输入" name="input" v-model="dataInfo.hydraulichressure"  class="input-align"  type="digit"></input></view>
            </view>
            <!-- <view class="cu-form-group ">
                <view class="title"><text class="cuIcon-favorfill text-xs text-red"></text
              >试压结果记录</view>
                <view><input placeholder="请输入试压结果记录" name="input" v-model="dataInfo.result"  class="input-align"></input></view>
            </view> -->
            <view class="cu-bar bg-white">
                <view class="action"><text class="cuIcon-favorfill text-xs text-red"></text
              >试压结果记录</view>
            </view>
            <view class="cu-form-group">
            <textarea
			        :disabled="isDisabled"
              maxlength="-1"
              @input="textareaAInput"
              placeholder="请输入"
              v-model="dataInfo.result"
            ></textarea>
          </view>
            <view class="cu-form-group" style="min-height: 0px;"></view>
            <view class="cu-bar bg-white">
                <view class="action"><text class="cuIcon-favorfill text-xs text-red"></text
              >试压附件</view>
            </view>
            <view class="cu-form-group ">
                <js-uploadfile :deletable="!isDisabled" :customBtn="isDisabled" v-model="dataInfo.dataMap"  :biz-key="dataInfo.id"  biz-type="pmPressuretest_image" ></js-uploadfile>
            </view>

            <!-- <file-upload :uploadOptions="uploadOptions" :imgList="dataInfo.shiYaFile" @imgListChange="imgListChange"></file-upload> -->

          <view style="height: 80px" ></view>
            <view class="btn-group cu-bar foot ">
                <js-bpm-button v-model="dataInfo.bpm"  :biz-key="dataInfo.id" form-key="Flow_PressureTest" @complete="submit" :showTraceText="showTraceText" ></js-bpm-button>
            </view>
            <!-- <view class="btn-group cu-bar foot " v-else-if="!dataInfo.id">
                <js-bpm-button v-model="dataInfo.bpm"  :biz-key="dataInfo.id" form-key="Flow_PressureTest" @complete="submit" ></js-bpm-button>
            </view> -->
        </form>
        <add-detail :modalNameDetail="modalNameDetail" @modalNameDetailEmit="modalNameDetailEmit" v-if="showAddDetail"></add-detail>
        
    </view>
</template>
<script>
import xmInfo from "@/pages/zfgs/index/componetns/xmInfo.vue";
import {hasPermission} from '@/common/fire.js'
export default {
  components: {
    xmInfo,
  },
  data() {
    return {
      switchA: false,
      date: "2024-03-12",
      uploadOptions: {},
      modalName: null,
      modalNameDetail: null,
      showAddDetail: false,
      dataInfo: {},
      showButton: true,
      isSubmitting: false,
      bpmTaskId: '',
      showTraceText: true,
      // flag: hasPermission('process:pressure:pmPressuretest:edit'),
      // addflag: hasPermission('btn:add:shiyachongxi'),
      // editflag: hasPermission('btn:edit:shiyachongxi')
    };
  },
  computed: {
    isDisabled() {
       const statusCondition = (!this.dataInfo.id && this.dataInfo.status == '4') || ['5', '6', '9'].includes(this.dataInfo.status);
       // const bpmTaskIdCondition = !!this.bpmTaskId;
       return !statusCondition ;
    }
  },
  onLoad(e) {
    console.log(e, "e");
    if (!!e.id) {
      console.log(e.id, "e.id");
      
      console.log(this.bpmTaskId, "this.bpmTaskId");
      this.showButton = true;
      this.getPressurePmPressuretestForm(e.id);
      this.bpmTaskId = e['bpm.taskId'];
      if (!this.bpmTaskId) {
        // this.dataInfo.bpm = {};
        showTraceText = false;
      }
      this.dataInfo.id = e.id
    }
    if (!!e.xm) {
      this.showButton = true;
      this.dataInfo = JSON.parse(e.xm);
      this.dataInfo.status = '4';
      //   巡检人
    }
    // this.dataInfo = JSON.parse(e.item)
  },
  methods: {

    submit() {
      if (!this.dataInfo.discharge) {
        this.$u.toast("水流量必填");
        return;
      }
      if (!this.dataInfo.hydraulichressure) {
        this.$u.toast("水压必填");
        return;
      }
      if (!this.dataInfo.result) {
        this.$u.toast("试压结果记录必填");
        return;
      }
      if (
        !this.dataInfo.dataMap ||
        !this.dataInfo.dataMap.pmPressuretest_image
      ) {
        this.$u.toast("试压附件必传");
        return;
      }
      if (this.isSubmitting) {
        return;
      } else {
        this.isSubmitting = true;
        // if (this.model.iqty == null || this.model.iqty == "") {
        //   this.$refs.jsError.showError("", "请输入数量", "error");
        //   this.isSubmitting = false;
        //   return;
        // }
		    this.dataInfo.status = '4'
        try {
          this.$u.api.zfgs
            .pressurePmPressuretestSave(this.dataInfo)
            .then((res) => {
              if (res.result == "true") {
                this.$u.toast(res.message);
                const itemContent = {
                  projectno: this.dataInfo.projectno,
                  projectname: this.dataInfo.projectname,
                };
                setTimeout(() => {
                  // uni.navigateTo({
                  //   url:
                  //     "/pages/zfgs/process/pressure/pmPressuretest/list?item=" +
                  //     JSON.stringify(itemContent),
                  // });
				  uni.navigateBack({
				    delta: 1,
				  });
                }, 500);
              } else {
                this.$u.toast(res.message);
              }
            });
        } catch (error) {
          this.$u.toast(error.message);
        } finally {
          this.isSubmitting = false;
        }
      }
    },

    getPressurePmPressuretestForm(id) {
      this.$u.api.zfgs
        .pressurePmPressuretestForm({
          id: id,
        })
        .then((res) => {
          this.dataInfo = res.pmPressuretest;
        });
    },
    SwitchA(e) {
      // this.switchA = e.detail.value
    },
    DateChange(e) {
      this.date = e.detail.value;
    },
    textareaAInput(e) {
      this.dataInfo.result = e.detail.value;
    },
    addDetail() {
      this.showAddDetail = true;

      this.modalNameDetail = "bottomModal";

      console.log(this.modalNameDetail);
    },
    modalNameDetailEmit(e) {
      this.modalNameDetail = e;
    },

    ViewImage(imageList, e) {
      uni.previewImage({
        urls: imageList,
        current: e.currentTarget.dataset.url,
      });
    },
    DelImg(e) {
      uni.showModal({
        title: "",
        content: "确定要删除吗？",
        cancelText: "取消",
        confirmText: "确认",
        success: (res) => {
          if (res.confirm) {
            console.log(
              res.confirm,
              this.dataInfo.dImageList,
              e,
              this.dataInfo.dImageList[e.currentTarget.dataset.index]
            );

            this.$delete(
              this.dataInfo.dImageListst,
              e.currentTarget.dataset.index
            );
          }
        },
      });
    },
    ChooseImage() {
      uni.chooseImage({
        count: 4, //默认9
        sizeType: ["original", "compressed"], //可以指定是原图还是压缩图，默认二者都有
        sourceType: ["album"], //从相册选择
        success: (res) => {
          if (this.dataInfo.shiYaFile.length != 0) {
            this.dataInfo.shiYaFile = this.dataInfo.shiYaFile.concat(
              res.tempFilePaths
            );
          } else {
            this.dataInfo.shiYaFile = res.tempFilePaths;
          }

          console.log(this.dataInfo, "this.dataInfo");
          this.$u.toast("上传成功");
        },
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.action {
  color: #303133;
}
.title {
  flex-shrink: 0;
}
.text-sl {
  font-size: 22px !important;
}
.cu-bar {
}
.cu-btn {
  font-size: 16px;
  height: 40px;
}
.foot {
  min-height: 80px;
  z-index: 997;
  background: #fff;
}
.green {
  color: green;
}
.input-align {
  text-align: right;
}
.text-xs {
  font-size: 18rpx !important;
}
</style>